﻿using Newtonsoft.Json;
using PMIS.Common;
using PMIS.Dialogs;
using PMIS.HttpRequest;
using PMIS.Shared.DTO;
using PMIS.Shared.Models;
using PMIS.Views.Department;
using System.Windows.Input;

namespace PMIS.ViewModels.Department
{
    public class DeptViewModel : ViewModelBase
    {
        private readonly IDialogService _dialogService;
        private readonly HttpService _httpService;
        public DeptViewModel(IDialogService dialogService, HttpService httpService)
        {
            _dialogService = dialogService;
            _httpService = httpService;

            LoadNavDept();
        }

        private void OnAdd()
        {
            _dialogService.ShowDialog(nameof(AddEditDept), null, AddEditCallBack);
        }

        private void OnDelete()
        {

        }

        private void OnEdit(object obj)
        {
            if (obj is DepartmentDto dept)
            {
                DialogParameters para = new DialogParameters();
                para.Add("data", dept.DeepClone());
                _dialogService.ShowDialog(nameof(AddEditDept), para, AddEditCallBack);
            }
        }

        private void OnPrint()
        {

        }

        private void OnRefresh()
        {

        }

        private void OnSelectedItem(object obj)
        {
            if (obj is DepartmentDto dept)
            {
                SelectedDeptList = new List<DepartmentDto>();
                SelectedDeptList = AllDeptList.Where(x => x.Id == dept.Id || x.ParentId == dept.Id).ToList();
            }
        }

        private ICommand _addCommand;
        public ICommand AddCommand => _addCommand ??= new DelegateCommand(OnAdd);

        private ICommand _editCommand;
        public ICommand EditCommand => _editCommand ??= new DelegateCommand<object>(obj => OnEdit(obj));

        private ICommand _deleteCommand;
        public ICommand DeleteCommand => _deleteCommand ??= new DelegateCommand(OnDelete);

        private ICommand _printCommand;
        public ICommand PrintCommand => _printCommand ??= new DelegateCommand(OnPrint);

        private ICommand _refreshCommand;
        public ICommand RefreshCommand => _refreshCommand ??= new DelegateCommand(OnRefresh);

        private ICommand _selectedItemChangedCommand;
        public ICommand SelectedItemChangedCommand => _selectedItemChangedCommand ??= new DelegateCommand<object>(obj => OnSelectedItem(obj));

        private void AddEditCallBack(IDialogResult dialogResult)
        {
            if (dialogResult != null && dialogResult.Result == ButtonResult.OK)
            {
                LoadNavDept();
            }
        }


        private List<DepartmentDto>? _allDeptList;
        public List<DepartmentDto>? AllDeptList
        {
            get
            {
                return _allDeptList;
            }
            set
            {
                SetProperty(ref _allDeptList, value);
            }
        }

        private List<DepartmentDto>? _selectedDeptList;
        public List<DepartmentDto>? SelectedDeptList
        {
            get
            {
                return _selectedDeptList;
            }
            set
            {
                SetProperty(ref _selectedDeptList, value);
            }
        }

        private List<DepartmentDto>? _treeDeptList;
        public List<DepartmentDto>? TreeDeptList
        {
            get
            {
                return _treeDeptList;
            }
            set
            {
                SetProperty(ref _treeDeptList, value);
            }
        }

        private async void LoadNavDept()
        {
            var response = await _httpService.GetAsync(Api.DEPT_GET_LIST);
            if (response?.code == 1)
            {
                var data = JsonConvert.DeserializeObject<PagingModel<DepartmentDto>>(JsonConvert.SerializeObject(response.data));
                AllDeptList = data.Items.OrderBy(p => p.Sort).ToList();

                LoadTreeDeptList();

                SelectedDeptList = AllDeptList;
            }
        }

        private void LoadTreeDeptList()
        {
            var deptList = AllDeptList;
            TreeDeptList = new List<DepartmentDto>(deptList.Where(x => x.ParentId == null));
            foreach (var item in TreeDeptList)
            {
                LoadChildDeptList(item);
            }
        }

        public void LoadChildDeptList(DepartmentDto dept)
        {
            var deptList = AllDeptList;
            dept.Children = deptList.Where(x => x.ParentId == dept.Id).ToList();
            foreach (var subitem in dept.Children)
            {
                // Recursive call
                LoadChildDeptList(subitem);
            }
        }
    }
}
